package com.yc.bigdata.hbase.config;

import com.yc.bigdata.hbase.config.props.HBaseProperties;
import com.yc.bigdata.hbase.core.HBaseTemplate;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Map;
import java.util.Set;

/**
 * <p></p>
 *
 * @author: YuanChilde
 * @date: 2020-01-16 13:41
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-01-16 13:41    YuanChilde     1.0        新增
 */
@org.springframework.context.annotation.Configuration
@EnableConfigurationProperties(HBaseProperties.class)
@ConditionalOnClass(HBaseTemplate.class)
public class HBaseConfig {

    @Autowired
    private HBaseProperties hBaseProperties;

    @Bean
    @ConditionalOnMissingBean(HBaseTemplate.class)
    public HBaseTemplate hbaseTemplate() {
        Configuration configuration = HBaseConfiguration.create();
        Map<String, String> config = hBaseProperties.getConfig();
        Set<String> keySet = config.keySet();
        for (String key : keySet) {
            configuration.set(key, config.get(key));
        }
        return new HBaseTemplate(configuration);
    }
}
